/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hu.tothgya.timetable.utils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

/**
 *
 * @author tothgya
 */
public class NonUTF8WMLConverter {

    public static Document createDocument(String urlstr, String data) {
        try {


            URL url = new URL(urlstr);
            URLConnection conn = url.openConnection();
            conn.setDoOutput(true);
            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
            wr.write(data);
            wr.flush();

            InputStream stream = url.openStream();

            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

            StringWriter w = new StringWriter();
            int b = 0;
            while ((b = stream.read()) > 0) {
                w.write(b);
            }
            Document d = builder.parse(new ByteArrayInputStream(w.toString().getBytes("UTF8")));

            return d;
        } catch (SAXException ex) {
            Logger.getLogger(NonUTF8WMLConverter.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(NonUTF8WMLConverter.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(NonUTF8WMLConverter.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MalformedURLException ex) {
            Logger.getLogger(NonUTF8WMLConverter.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(NonUTF8WMLConverter.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public static Document createDocument(String urlstr) {
        try {
            URL url = new URL(urlstr);
            InputStream stream = url.openStream();
            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

            StringWriter w = new StringWriter();
            int b = 0;
            while ((b = stream.read()) > 0) {
                w.write(b);
            }
            Document d = builder.parse(new ByteArrayInputStream(w.toString().getBytes("UTF8")));
            return d;
        } catch (SAXException ex) {
            Logger.getLogger(NonUTF8WMLConverter.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(NonUTF8WMLConverter.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(NonUTF8WMLConverter.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MalformedURLException ex) {
            Logger.getLogger(NonUTF8WMLConverter.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(NonUTF8WMLConverter.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}
